
export let File = {
  getOpenFileName(accept = "*", cb) {
    // 打开的文件路径
    let fileName = SSmap.getOpenFileName(accept);
    cb(fileName);
  },
  getExistingDirectory(cb, accept = "*") {
    //获取当前磁盘路径
    let fileName = SSmap.getExistingDirectory(accept);
    cb(fileName);
  },
  // 通过相对路径获取绝对路径
  getAbsolutePathAndData(path, cb) {
    let relativePath = SSmap.getAbsolutePath(path, true);
    cb && cb(relativePath);
    return relativePath;
  },
  // 保存路径（可新建文件）
  getSaveFileName(accept = "数生文件(*.ssmap)", cb) {
    let fileName = SSmap.getSaveFileName(accept);
    fileName && cb(fileName);
  },
  readFile(filename, cb) {
    // 获取目标文件file信息
    let data = SSmap.readFile(filename, false);
    let str = Utf8ArrayToStr(data);
    cb(data)
    function Utf8ArrayToStr(array) {
      var out, i, len, c;
      var char2, char3;

      out = "";
      len = array.length;
      i = 0;
      while (i < len) {
        c = array[i++];
        switch (c >> 4) {
          case 0:
          case 1:
          case 2:
          case 3:
          case 4:
          case 5:
          case 6:
          case 7:
            // 0xxxxxxx
            out += String.fromCharCode(c);
            break;
          case 12:
          case 13:
            // 110x xxxx 10xx xxxx
            char2 = array[i++];
            out += String.fromCharCode(
              ((c & 0x1f) << 6) | (char2 & 0x3f)
            );
            break;
          case 14:
            // 1110 xxxx 10xx xxxx 10xx xxxx
            char2 = array[i++];
            char3 = array[i++];
            out += String.fromCharCode(
              ((c & 0x0f) << 12) |
              ((char2 & 0x3f) << 6) |
              ((char3 & 0x3f) << 0)
            );
            break;
        }
      }

      return out;
    }
  },
  // 读文件（字符串
  readFileAsString(filename, isEncode, cb) {
    let str = SSmap.readFileAsString(filename, isEncode);
    cb(str);
  },
  // 写文件
  writeFile(filename, { isEncode = true, data }, cb) {
    SSmap.writeFile(filename, data, isEncode);
    cb();
  },
};
